// CREATE TABLE `t_topic` (
//     `id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
//     `tid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '话题id',
//     `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标题',
//     `uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '创建者id',
//     `cid` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '分类id',
//     `imgs` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '图片',
//     `begin_date` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '创建时间',
//     `end_date` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '过期时间',
//     PRIMARY KEY (`id`,`tid`,`uid`,`cid`) USING BTREE
//   ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

let { v4: uuidv4 } = require("uuid")
let knex = require("../dao")
let TNAME = "t_topic";

class Topic {
    Add = (data, callback) => {
        data.tid = uuidv4()
        data.uid = "szw";
        data.cid = "001"
        data.begin_date = new Date()
        knex(TNAME).insert(data).then(res => {
            callback(undefined, { code: 0, msg: "添加成功", data: {} })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Update = (data, callback) => {
        let { tid } = data
        data.begin_date = new Date()
        knex(TNAME).where({ tid }).update(data).then(res => {
            callback(undefined, { code: 0, msg: "更新成功", data: {} })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    List = ({ page, page_size }, callback) => {
        knex(TNAME).select().limit(page_size || 10).offset(page || 0).then(res => {
            callback(undefined, { code: 0, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Detail = ({ tid }, callback) => {
        knex(TNAME).select().where({ tid }).then(res => {
            callback(undefined, { code: 0, msg: "", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })
    }
    Remove = ({tid}, callback) => { 
        knex(TNAME).where({ tid }).del().then(res => {
            callback(undefined, { code: 0, msg: "删除成功", data: res })
        }).catch(err => {
            callback(undefined, { code: 500, msg: err, data: {} })
        })

    }
}

module.exports = Topic